<template>
    <div class="message">
        <div class="date-warp">
            <div class="date">{{formatTime(msg.sendTime)}}</div>
        </div>
        <div :class="'msg-warp' + (msg.sendUser.email === $store.state.user.email ? ' msg-warp-right' : ' msg-warp-left')">
            <!-- 头像，点击触发个人信息 -->
            <el-popover placement="top-start" width="330" trigger="click">
                <user-info-com :user="msg.sendUser" />
                <div class="avatar" slot="reference">
                    <el-image :src="msg.sendUser.avatar" fit="cover" />
                </div>
            </el-popover>
            <div class="msg-content">
                <!-- 文字消息 -->
                <div class="text" v-if="msg.type === 0">{{msg.content}}</div>
                <!-- 图片消息 -->
                <div class="img" v-else-if="msg.type === 1">
                    <pic-msg-com :content="msg.content" />
                </div>
                <!-- 音频消息 -->
                <div class="audio" v-else-if="msg.type === 2">
                    <audio-msg-com :content="msg.content" :isMineSend="msg.sendUser.email === $store.state.user.email" />
                </div>
                <!-- 视频消息 -->
                <div class="video" v-else-if="msg.type === 3">
                    <video-msg-com :content="msg.content" />
                </div>
                <!-- 其他文件消息 -->
                <div class="file" v-else>
                    <other-file-msg-com :content="msg.content" :filename="msg.filename" :size="msg.size" />
                </div>
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import {Vue,Component,Prop} from 'vue-property-decorator'
import UserInfoCom from './UserInfoCom.vue'
import PicMsgCom from './msg_coms/PicMsgCom.vue'
import AudioMsgCom from './msg_coms/AudioMsgCom.vue'
import VideoMsgCom from './msg_coms/VideoMsgCom.vue'
import OtherFileMsgCom from './msg_coms/OtherFileMsgCom.vue'
import moment from 'moment'

@Component({
    components:{
        UserInfoCom,PicMsgCom,AudioMsgCom,VideoMsgCom,OtherFileMsgCom
    }
})
export default class MessageCom extends Vue {

    // 消息主体
    @Prop({type:Object, required:true, default:{}})
    public msg:any

    
    // 格式化消息事件
    public formatTime(date:string){
        moment.locale('zh-cn'); 
        return moment(date).calendar()
    }
    
}
</script>

<style lang="less" scoped>
 .message{
     padding: 0px 20px;
     width: 100%;
     .date-warp{
         width: 100%;
         text-align: center;
         padding: 10px;
         .date{
             display: inline-block;
             padding: 5px;
             background-color: #DADADA;
             border-radius: 2px;
             color: white;
             font-size: 10px;
         }
     }
     // 公共消息主体样式
     .msg-warp{
         width: 100%;
         display: flex;
         padding-bottom: 10px;
         .avatar{
            width: 40px;
            height: 40px;
            cursor: pointer;
            .el-image{
                widows: 100%;
                height: 100%;
            }
        }
        .msg-content{
            min-width: 40px;
            max-width: 50%;
            padding: 10px;
            font-size: 14px;
            border-radius: 5px;
            border: 1px solid #efefef;
            &::before{
                content: '';
                width: 0;
                height: 0;
                transform: translateY(-50%);
            }
            // 文字消息
            .text{
                user-select: text;
            }
            & > div{
                max-width: 100%;
            }
        }
     }
     // 左侧消息样式
     .msg-warp-left{
        .msg-content{
            position: relative;
            margin-left: 10px;
            background-color: white;
            &::before{
                border: 5px solid #fff;
                border-color: transparent #fff transparent transparent;
                position: absolute;
                right: 100%;
                top: 20px;
            }
        }
     }
     // 右侧消息样式
     .msg-warp-right{
         flex-direction: row-reverse;
         .msg-content{
            position: relative;
            margin-right: 10px;
            background-color: #9EEA6A;
            &::before{
                border: 5px solid #9EEA6A;
                border-color: transparent transparent transparent #9EEA6A;
                position: absolute;
                left: 100%;
                top: 20px;
            }
        }
     }
 }   
</style>